﻿package com.potocrewz.buttons
{
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import gs.easing.Sine;
	import gs.TweenMax;
	
	/**
	 * ...
	 * @author Adrien Lamotte
	 */
	public class TextButton extends MovieClip
	{
		
		private var _texte:TextField;
		private var _name:String;
		private var _color:uint;
		private var _colorOver:uint;
		private var _tint:uint;
		private var _duration:Number;
		
		private var _taille:int;
		
		private var _url:String;
		private var _typo:String;
		private var _letterSpacing:int;
		private var _autoSize:String;
		private var _target:String;
		
		public function TextButton(pName:String = "Menu", pURL:String = "", pTaille:int = 12, pDuration:Number = 0.4, pColor:uint = 0xFFFFFF, pColorOver:uint = 0x000000, pTypo:String = "Verdana", pLetterSpacing:int = 0, pAutoSize:String = TextFieldAutoSize.LEFT,pTarget:String = "_self") 
		{
			_name = pName;
			_url = pURL;
			_taille = pTaille;
			_color = pColor;
			_colorOver = pColorOver;
			_letterSpacing = pLetterSpacing;
			_duration = pDuration;
			_autoSize = pAutoSize;
			_target = pTarget;
			_typo = pTypo;
			
			mise_en_forme_texte();
			
			
			mouseChildren = false;
			
			
			this.addEventListener(Event.ADDED_TO_STAGE, addedHandler);
		}
		
		private function addedHandler(e:Event):void 
		{
			addEventListener(MouseEvent.MOUSE_OVER, _mouseLangueHandler);
			addEventListener(MouseEvent.MOUSE_OUT, _mouseLangueHandler);
			
			if(_url != "" && _url != null){
				addEventListener(MouseEvent.CLICK, _mouseLangueHandler);
				buttonMode = true;
			}
			
			this.addEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
		}
		
		private function removeHandler(e:Event):void 
		{
			removeEventListener(MouseEvent.MOUSE_OVER, _mouseLangueHandler);
			removeEventListener(MouseEvent.MOUSE_OUT, _mouseLangueHandler);
			
			if(_url != "" && _url != null){
				removeEventListener(MouseEvent.CLICK, _mouseLangueHandler);
			}
			this.removeEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
			
			this.addEventListener(Event.ADDED_TO_STAGE, addedHandler);
		}
		
		
		private function _mouseLangueHandler(e:MouseEvent):void 
		{
			switch(e.type) {
				case MouseEvent.MOUSE_OVER:
					_tint = _colorOver;
					changeColor(_tint);
				break;
				
				case MouseEvent.MOUSE_OUT:
					_tint = _color;
					changeColor(_tint);
				break;
				
				case MouseEvent.CLICK:
					navigateToURL(new URLRequest(_url),_target);
				break;
			}
			
			//dispatchEvent(new MouseEvent(e.type, true, false));
		}
		
		private function changeColor(color:uint):void {
			//TweenMax.to(this, _duration, {colorMatrixFilter:{colorize:color, amount:1}, ease:Sine.easeOut});
			TweenMax.to(this, _duration, {tint:color, ease:Sine.easeOut});
		}
		
		
		
		private function mise_en_forme_texte():void
		{
			_texte = new TextField();
			_texte.autoSize = _autoSize;
			_texte.textColor = _color;
			_texte.embedFonts = true;
			_texte.selectable = false;
			
			_texte.antiAliasType = AntiAliasType.ADVANCED;
			
			var format:TextFormat = new TextFormat();
			format.letterSpacing = _letterSpacing;
			format.size = _taille;
			format.font = _typo;
			
            _texte.defaultTextFormat = format;
			_texte.htmlText = _name;
			
			
			addChild(_texte);
		}
	
		
	}
	
}